package com.woniuxy.web;

import com.woniuxy.config.ResponseResult;
import com.woniuxy.entity.User;
import com.woniuxy.service.UserService;
import com.woniuxy.untis.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private JwtUtils jwtUtils;

    @Autowired
    private UserService userService;
    @PostMapping("login")
    public ResponseResult<User> login(String user_Name , String password, HttpServletResponse response){
        User user = userService.login(user_Name);
        if(user!=null){
            if(user.getPassword().equals(password)){
                String jwt = jwtUtils.createJwt(user.getUserId(),user.getUserName());
                response.addHeader("Access-Control-Expose-Headers","token");
                response.addHeader("token",jwt);
                return new ResponseResult<>(200,"ok",user);
            }else {
                return new ResponseResult<>(407,"密码错误");
            }
        }else {
            return new ResponseResult<>(406,"账号不存在");
        }
    }

    @PostMapping("cinemaAdd/{userId}/{cinemaId}")
    public ResponseResult<Void> cinemaAdd(@PathVariable("userId") int userId,@PathVariable("cinemaId") int cinemaId){
        userService.cinemaAdd(userId,cinemaId);
        return ResponseResult.SUCCUESS;
    }
}